package org.peterbjornx.formats;

import org.peterbjornx.io.AbstractFileStore;
import org.peterbjornx.io.RSInputStream;

import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: PeterbjornX
 * Date: Apr 9, 2009
 * Time: 8:56:35 PM
 * >
 */
public final class FloorCollection {
    public String name;
    private static final boolean LOAD_NE = true;
    public int colour3;
    public int colour1;

    public static void unpackConfig(AbstractFileStore jagexArchive) throws IOException {
        RSInputStream stream = jagexArchive.getFile("flo.dat");
        int cacheSize = stream.readUShort();
        if(cache == null)
            cache = new FloorCollection[cacheSize];
        for(int j = 0; j < cacheSize; j++)
        {
            if(cache[j] == null)
                cache[j] = new FloorCollection();
            cache[j].readValues(stream);
        }
    }

    private void readValuesHD(RSInputStream stream) throws IOException {
        do
        {
            int opcode = stream.readByte();
            if(opcode == 0)
                return;
            else if(opcode == 1)
            {
                stream.read3Bytes();
            } else if(opcode == 2){
                texture = stream.readByte();
                //premixedtex = getTexturePixels(texture);
            }
            else if(opcode == 3) //Replaced by texture_word in HD
            {
                texture = stream.readUShort();
                if (texture == 65535)
                    texture = -1;
                if (texture != -1)
                {
                    //premixedtex = getTexturePixels(texture);
                    /*if (texture > 49)
                    {
                        for (int idx = 0;idx < premixedtex.length;idx++)
                        premixedtex[idx] = ColorUtil.blend(new Color(premixedtex[idx]),new Color(colour2),0.6d).getRGB();
                    } */
                }
            } else if(opcode == 5)
                aBoolean393 = false;
            else if(opcode == 6)
                name = stream.readString10T();
            else if(opcode == 7)
            {
                stream.read3Bytes();
            }
            else if (opcode == 8 || opcode == 10 || opcode == 12) {
                //Nothing
            }
            else if (opcode == 9) {
                stream.readUShort();
            }
            else if (opcode == 11 || opcode == 14) {
                stream.readByte();
            }
            else if (opcode == 13)
            {
                stream.read3Bytes();
            }
            else
            {
                System.err.println("[FloorData] Error unrecognised new engine config code: " + opcode);
            }
        } while(true);
    }

    private void readValues(RSInputStream stream) throws IOException {
        do
        {
            int i = stream.readByte();
            boolean dummy;
            if(i == 0)
                return;
            else
            if(i == 1)
            {
                colour2 = stream.read3Bytes();
                method262(colour2);
            } else
            if(i == 2)
                texture = stream.readByte();
            else
            if(i == 3) //Replaced by texture_word in HD
                dummy = true;
            else
            if(i == 5)
                aBoolean393 = false;
            else
            if(i == 6)
                name = stream.readString10T();
            else
            if(i == 7)
            {
                int j = anInt394;
                int k = pCB;
                int l = pCC;
                int i1 = pCA;
                int j1 = colour1 = stream.read3Bytes();
                method262(j1);
                anInt394 = j;
                pCB = k;
                pCC = l;
                pCA = i1;
                pCDivider = i1;
            } else
            {
                System.out.println("Error unrecognised config code: " + i);
            }
        } while(true);
    }

    private void method262(int i)
    {
        double d = (double)(i >> 16 & 0xff) / 256D;
        double d1 = (double)(i >> 8 & 0xff) / 256D;
        double d2 = (double)(i & 0xff) / 256D;
        double d3 = d;
        if(d1 < d3)
            d3 = d1;
        if(d2 < d3)
            d3 = d2;
        double d4 = d;
        if(d1 > d4)
            d4 = d1;
        if(d2 > d4)
            d4 = d2;
        double d5 = 0.0D;
        double d6 = 0.0D;
        double d7 = (d3 + d4) / 2D;
        if(d3 != d4)
        {
            if(d7 < 0.5D)
                d6 = (d4 - d3) / (d4 + d3);
            if(d7 >= 0.5D)
                d6 = (d4 - d3) / (2D - d4 - d3);
            if(d == d4)
                d5 = (d1 - d2) / (d4 - d3);
            else
            if(d1 == d4)
                d5 = 2D + (d2 - d) / (d4 - d3);
            else
            if(d2 == d4)
                d5 = 4D + (d - d1) / (d4 - d3);
        }
        d5 /= 6D;
        anInt394 = (int)(d5 * 256D);
        pCB = (int)(d6 * 256D);
        pCC = (int)(d7 * 256D);
        if(pCB < 0)
            pCB = 0;
        else
        if(pCB > 255)
            pCB = 255;
        if(pCC < 0)
            pCC = 0;
        else
        if(pCC > 255)
            pCC = 255;
        if(d7 > 0.5D)
            pCDivider = (int)((1.0D - d7) * d6 * 512D);
        else
            pCDivider = (int)(d7 * d6 * 512D);
        if(pCDivider < 1)
            pCDivider = 1;
        pCA = (int)(d5 * (double) pCDivider);
        int k = (anInt394 + (int)(Math.random() * 16D)) - 8;
        if(k < 0)
            k = 0;
        else
        if(k > 255)
            k = 255;
        int l = (pCB + (int)(Math.random() * 48D)) - 24;
        if(l < 0)
            l = 0;
        else
        if(l > 255)
            l = 255;
        int i1 = (pCC + (int)(Math.random() * 48D)) - 24;
        if(i1 < 0)
            i1 = 0;
        else
        if(i1 > 255)
            i1 = 255;
        anInt399 = method263(k, l, i1);
    }

    private int method263(int i, int j, int k)
    {
        if(k > 179)
            j /= 2;
        if(k > 192)
            j /= 2;
        if(k > 217)
            j /= 2;
        if(k > 243)
            j /= 2;
        return (i / 4 << 10) + (j / 32 << 7) + k / 2;
    }

    private FloorCollection()
    {
        texture = -1;
        aBoolean393 = true;
    }

    public static FloorCollection cache[];
    public int colour2;
    public int texture;
    public boolean aBoolean393;
    public int anInt394;
    public int pCB;
    public int pCC;
    public int pCA;
    public int pCDivider;
    public int anInt399;
    public int[] premixedtex;
}

